package pkg;


import java.util.ArrayList;
import java.util.Scanner;


import java.util.ArrayList;

public class Controller {

	ArrayList<Product> product_list;
	ArrayList<Product> order_list;
	public Controller()
	{
		product_list = new ArrayList<Product>();
		order_list = new ArrayList<Product>();
		product_list.add(new Beverage());
		product_list.add(new Ice_Cream());
		product_list.add(new Sundae());
	}

	public void welcomeMessage()
	{
		System.out.println("Hello, welcome to the Baskin Robbins Training Software.");
	}

	public void selectionPrompt()
	{
		Scanner in = new Scanner(System.in);
		int selection;
		int counter = 0;
		String additional;
		boolean finished = false;
		while(!finished)
		{
			System.out.println("Please choose an item you would like to prepare:");

			selection = 0;
			for(Product p : product_list)
			{
				counter++;
				System.out.println("\t"+counter+" - "+p.getGenericLabel());
			}
			selection = in.nextInt();
			while(selection <= 0 || selection > product_list.size())
			{
				System.out.println("Invalid selection, please try again:");
				selection = in.nextInt();
			}
			Product prod = product_list.get(selection - 1).submenuPrompt();
			order_list.add(prod);
			System.out.println("Preparing " + prod.getName() + "\n");
			
			
			System.out.print("Would the cusomter like anything else? (Y/N)");
			additional = in.next();
			if(additional.equalsIgnoreCase("Y"));
			{
				finished = true;
			}	
		}		
	}

	public double calcTotalCost()
	{
		if(order_list.isEmpty())
		{
			return 0.0;
		}

		double total = 0.0;
		for(Product p : order_list)
		{
			total += p.getPrice();
		}
		return total;
	}

	public void paidInterface()
	{
		Scanner in = new Scanner(System.in);
		double total = calcTotalCost();
		System.out.printf("Receive payment of $%5.2f from customer.\n", total);
		System.out.print("How much did the customer pay?");
		String paid_str = in.next();
		Double paid = Double.parseDouble(paid_str);

		while(paid < total)
		{
			System.out.println("\nCustomer did not pay enough, ask for more");
			System.out.print("How much did the customer pay?");
			paid = in.nextDouble();
		}
		double change = paid - total;
		System.out.printf("\nReceived $%5.2f", paid);
		System.out.printf("\nMake change of %5.2f for customer", change);
		System.out.println("\nPresent product to customer");		
	}

	public void execute()
	{
		welcomeMessage();
		selectionPrompt();
		paidInterface();
		System.out.println("Thank you for using the Baskin Robbins Training Program.");
	}
	public static void main(String[] args)
	{
		Controller c = new Controller();
		c.execute();
	}
}
